package com.space.jwt.service;

import com.space.jwt.Dto.ManagerInfo;
import com.space.jwt.Dto.SysRole;
import com.space.jwt.entities.Permission;
import com.space.jwt.entities.SysUser;
import com.space.jwt.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 后台用户管理
 */

@Service
@Slf4j
public class ManagerInfoService {

    @Autowired
    SysUserMapper sysUserMapper;

    /**
     * 通过名称查找用户
     * 这里我直接写常量，实际生产环境会通过DAO访问数据库
     *
     * @param username
     * @return
     */
    public ManagerInfo findByUsername(String username) {
       Map<String,Object> map = new HashMap<>();
       map.put("username",username);
        List<SysUser> sysUsers = sysUserMapper.selectByMap(map);
        if(sysUsers.size()>0){
            ManagerInfo managerInfo = new ManagerInfo();

            managerInfo.setUsername(sysUsers.get(0).getUsername());
            managerInfo.setPids("1,2,3");
            managerInfo.setPidsList(Arrays.asList("1", "2", "3"));
            managerInfo.setPnames("第1个,第2个");
            managerInfo.setState(sysUsers.get(0).getState());
            managerInfo.setCreatedTime(sysUsers.get(0).getCreatedTime());
            managerInfo.setName(sysUsers.get(0).getName());
            // 随机数
            managerInfo.setSalt(sysUsers.get(0).getSalt());
            managerInfo.setPassword(sysUsers.get(0).getPassword());
            List<SysRole> roles = new ArrayList<>();
            SysRole role = new SysRole();
            role.setId("1");
            role.setRoleName("admin");
            List<Permission> permissions = new ArrayList<>();
            for(int i = 0; i<4;i++){
                Permission permission = new Permission();
                permission.setId(i+"");
                permission.setPermission(i+"111");
                permissions.add(permission);
            }
            role.setPermissions(permissions);
            role.setDescription(sysUsers.get(0).getTips());
            roles.add(role);
            managerInfo.setRoles(roles);
            return managerInfo;
        }else{
            return null;
        }
    }

}
